package com.oofetch.baidu.ws.net114;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.oofetch.baidu.dao.model.ModelOrgInformation;
import com.oofetch.baidu.util.HttpConnector;

public class OrgInfomationFetcher {


	public static ModelOrgInformation fetchOrgInfo(String url){


		ModelOrgInformation org = null;
		Document doc;

		String contactUrl = url + "/contactus.html";
		//String introUrl = url +  "/intro.html";

		try {
			org = new ModelOrgInformation();
			org.setRefernceUrl(url);


			//先查看此网页是否有联系信息
			doc = HttpConnector.genConntion(url).get();
			Elements els = doc.select("div.detail_title_h35");
			
			Element el = null;
			if (els.size() > 0) {

				els = els.first().children();

				org.setOwner(els.first().text());

				org.setMobile(els.get(1).text().replaceAll("手        机：", ""));
				org.setTel(els.get(2).text().replaceAll("联系电话：", ""));
				org.setWebsite(els.get(6).text().replaceAll("公司主页：", ""));

				el = doc.select("div.detail_title_h35").first().previousElementSibling();
				org.setTitle(el.text());
			}
			else{

				//int sleepSec = Integer.valueOf(System.getProperty("SYS_SLEEP_SEC"));
				//Thread.sleep(sleepSec * 1000);


				doc = HttpConnector.genConntion(contactUrl).get();

				els =  doc.select("html body div.frame div.r_w760 div.p_10_0 form#form table.table tbody tr td p");

				if (els.size()>4) {

					org.setTitle(els.select(":containsOwn(公司名称)").text().replaceFirst("公司名称：", ""));
					org.setOwner(els.select(":containsOwn(联系人)").text().replaceFirst("联系人：", ""));
					org.setTel(els.select(":containsOwn(联系电话)").text().replaceFirst("联系电话：", ""));
					org.setMobile(els.select(":containsOwn(联系电话)").text().replaceFirst("手机：", ""));
					org.setWebsite(els.select(":containsOwn(公司主页)").text().replaceFirst("公司主页：", ""));
				}
				else{

					el =  doc.select("html body div.w div.main div.com-r-title div.com-r-con div.contact-list").first();
					if(el == null){
						el = doc.select("p:containsOwn(公司名称：)").first();
						org.setTitle(el.childNodes().get(0).toString().replaceAll("公司名称：", ""));

						org.setOwner(el.childNodes().get(2).toString().replaceAll("联系人：", ""));

						org.setTel(el.childNodes().get(2).toString().replaceAll("联系电话：", ""));
						org.setMobile(el.childNodes().get(4).toString().replaceAll("手机：", ""));
						org.setWebsite(el.childNodes().get(9).attr("href").toString());
					}
					else{

						org.setOwner(el.childNodes().get(0).toString().replaceAll("联系人：", ""));
						org.setTel(el.childNodes().get(2).toString().replaceAll("联系电话：", ""));
						org.setMobile(el.childNodes().get(4).toString().replaceAll("手机：", ""));
						org.setWebsite(el.childNodes().get(9).attr("href").toString());
					}

				}
				

				//有些公司没有录入主营项目
				try{
					doc = HttpConnector.genConntion(url).get();

					String zyStr =  doc.select("html body div.frame div.r_w760 div.p_10_0 dl dd h1").text();
					zyStr = zyStr.replaceAll("主营：", "");

					org.setDes( zyStr);
				}catch (Exception e) {

				}
	 

 			}
			

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (org.getTitle() == null) {
			System.out.println("null:" + url);
		}

		return org;
	}

	public static void main(String[] args) {
		System.out.println(fetchOrgInfo("http://detail.net114.com/gongsi/1665208.html"));
	}
}
